//    Copyright (C) Kherty.  All rights reserved.
using System.ComponentModel;
using System.Diagnostics.Contracts;
using System.Reflection;
using OpenLS.Core.Serialization;

namespace OpenLS.Spreadsheet
{
    public class PageMargins : IOfficeXmlSerializable, INotifyPropertyChanged
    {
        private static double FromInches(double d)
        {
            return d * 96;
        }
    
        internal static PageMargins NormalMargins
        {
            get
            {
                return new PageMargins
                {
                    Left = FromInches(0.7),
                    Top = FromInches(0.75),
                    Right = FromInches(0.7),
                    Bottom = FromInches(0.75),
                    Header = FromInches(0.3),
                    Footer = FromInches(0.3)
                };

            }
        }
        #region IOfficeXmlSerializable Members

        void IOfficeXmlSerializable.ReadXml(ReadContext context)
        {
            using (ReadContext c = context.Read("pageMargins"))
            {
                Left = c.GetDouble("left") * factor;
                Right = c.GetDouble("right") * factor;
                Top = c.GetDouble("top") * factor;
                Bottom = c.GetDouble("bottom") * factor;
                Header = c.GetDouble("header") * factor;
                Footer = c.GetDouble("footer") * factor;
            }
        }

        private const double factor = 96;

        void IOfficeXmlSerializable.WriteXml(WriteContext context)
        {
            using (WriteContext c = context.Write("pageMargins"))
            {
                c.SetDouble("left", Left / factor);
                c.SetDouble("right", Right / factor);
                c.SetDouble("top", Top / factor);
                c.SetDouble("bottom", Bottom / factor);
                c.SetDouble("header", Header / factor);
                c.SetDouble("footer", Footer / factor);
            }
        }

        #endregion
        private double left;

        public double Left
        {
            get { return left; }
            set { left = value; notifyPropertyChanged("Left"); }
        }
        private double right;

        public double Right
        {
            get { return right; }
            set { right = value; notifyPropertyChanged("Right"); }
        }
        private double top;

        public double Top
        {
            get { return top; }
            set { top = value; notifyPropertyChanged("Top"); }
        }
        private double bottom;

        public double Bottom
        {
            get { return bottom; }
            set { bottom = value; notifyPropertyChanged("Bottom"); }
        }
        private double footer;

        public double Footer
        {
            get { return footer; }
            set { footer = value; notifyPropertyChanged("Footer"); }
        }
        private double _header;

        public double Header
        {
            get { return _header; }
            set { _header = value;
                notifyPropertyChanged("Header"); }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        void notifyPropertyChanged(string propertyName)
        {
            Contract.Requires(GetType().GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) != null);
            if (PropertyChanged != null)
                PropertyChanged(null, new PropertyChangedEventArgs(propertyName));
        }

    }
}
